package org.itboys.mobile.service.mongo.web.cookbook;

import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.mobile.dao.product.SundayProductMapper;
import org.itboys.mobile.entity.mongo.cookbook.KitchenCookBookProduct;
import org.itboys.mobile.entity.mysql.product.SundayProduct;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：zzq
 * 时间：2017年5月9日
 * 描述：花果山（农厨）_菜谱_食材_service
 */
@Service
public class KitchenCookBookProductService extends BaseMongoService<KitchenCookBookProduct> {
    @Autowired
    private SundayProductMapper productMapper;
    @Resource(name = "kitchenDs")
    private MongoDataSource kitchenDataSource;

    @Override
    protected MongoDataSource getMongoDataSource() {
        return kitchenDataSource;
    }
    @Override
    protected Class<KitchenCookBookProduct> getEntityClass() {
        return KitchenCookBookProduct.class;
    }

    /**
     * 分页获取菜谱-食材
     * @param request
     * @return
     */
    public PageResult<KitchenCookBookProduct> selectCookBookProduct(HttpServletRequest request,Long cookBookId){
        Map<String,Object> param= new HashMap<String,Object>();
        param.put("isDeleted", 0);
        param.put("cookBookId",cookBookId);
        Map<String,String> containsparam = ParamUtil.packageMongoVagueParam(request,"");
        PageResult<KitchenCookBookProduct> result = super.containpageQuery(request,containsparam,param);
        result.setData(packageDtoMuilt(result.getData()));
        return result;
    }

    /**
     * 新增或者编辑食材
     * @param cookBookProduct
     */
    public void saveCookBookProduct(KitchenCookBookProduct cookBookProduct){
        if(cookBookProduct.getId()==0l){
            super.save(cookBookProduct);
        }else{
            super.updateExceptEmpty(cookBookProduct.getId(), cookBookProduct);

        }
    }
    /**
     * 查找单一菜谱实体
     * @param cookBookId
     * @return
     */
    public KitchenCookBookProduct findOne(Long id,Long cookBookId){
        KitchenCookBookProduct cookBookProduct=null;

        if(LongUtil.isNotZreo(id)){
            cookBookProduct=super.getById(id);
            cookBookProduct=packageDtoSingle(cookBookProduct,null);
        }else{
            cookBookProduct=new KitchenCookBookProduct();
            cookBookProduct.setId(0);
            cookBookProduct.setCookBookId(cookBookId);
        }
        return cookBookProduct;
    }
    /**
     * 组装DTO
     * @param bookProduct
     * @return
     */
    public KitchenCookBookProduct packageDtoSingle(KitchenCookBookProduct bookProduct,SundayProduct product){
        Map<String,Object> param = new HashMap<String,Object>();
        if(product==null){
            product = productMapper.find(bookProduct.getProductId());
        }
        bookProduct.setName(product.getName());
        bookProduct.setNumber(product.getNumber());
        bookProduct.setStandard(product.getStandard());
        bookProduct.setPrice(product.getPrice());
        return bookProduct;
    }
    public List<KitchenCookBookProduct> packageDtoMuilt(List<KitchenCookBookProduct> bookProducts) {
        List<KitchenCookBookProduct> results = new ArrayList<KitchenCookBookProduct>();
        List<Long> productIds = new ArrayList<Long>();
        for (KitchenCookBookProduct bookroduct : bookProducts) {
            productIds.add(bookroduct.getProductId());
        }
        if (ListUtil.isNotNull(productIds)) {
            Map<String, Object> param = new HashMap<String, Object>();
            param.put("ids", productIds);

            List<SundayProduct> products = new ArrayList<SundayProduct>();
            products = productMapper.select(param);
            for (KitchenCookBookProduct bookProduct : bookProducts) {
                for (SundayProduct product : products) {
                    if (LongUtil.isNotZreo(bookProduct.getProductId()) &&
                            bookProduct.getProductId().longValue() == product.getId().longValue()) {

                        results.add(packageDtoSingle(bookProduct,product));
                        break;
                    }
                }
            }


        }
        return results;
    }

}
